import echarts from 'echarts';
// 引入柱状图
import 'echarts/lib/chart/bar';
// 引入提示框和标题组件
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';
import storageApi from '../../../core/storage/storage';

function supervisorMonthPatrolController($rootScope, $scope, homeStatsService) {
  'ngInject';

  /*eslint-disable */
  this.getStatsPatrolSituation = () => {
    this.hasData = false;
    const param = {
       orgId: $rootScope.orgId || storageApi.get('orgId'),
    };
    homeStatsService
      .getStatsPatrolSituation(param)
      .then((res) => {
        if (res.success && res.data) {
          this.hasData = true;
          this.initChart(res.data);
        }
      });
  };

  this.initChart = (data) => {
    // const nums = data.map((item) => {
    //   return item.total;
    // });
    var nums = [data.patrolNum, data.problemNum];
    const option = {
      color: ['#38a7f0'],
      tooltip : {
      trigger: 'axis',
        axisPointer : { // 坐标轴指示器，坐标轴触发有效
          type : 'shadow', // 默认为直线，可选为：'line' | 'shadow'
        },
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis : [
        {
          type : 'category',
          data : ['检查数量', '问题数量'],
          axisTick: {
            alignWithLabel: true
          },
        },
      ],
      yAxis : [
        {
          type : 'value'
        }
      ],
      series : [
        {
          name:'数量',
          type:'bar',
          label: {
            normal: {
              show: true,
              position: 'top'
            }
          },
          barWidth: '30%',
          data: nums,
        }
      ],
    };
    this.myChart = echarts.init(document.getElementById('supervisor-month-patrol'));
    this.myChart.setOption(option);
  }

  $scope.$watch('$supervisorMonthPatrolCtrl.hasData', (newVal) => {
    if (!newVal) {
      setTimeout(() => {
        this.myChart.resize({
          width: 'auto',
          height: 'auto',
        });
      }, 500);
    }
  });

  this.getStatsPatrolSituation();
}

export default supervisorMonthPatrolController;
